//
//  CLCollectViewController.m
//  chanlin
//
//  Created by Ryan on 14-12-20.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLCollectViewController.h"
#import "IFCoreDataManager.h"
#import "CLCollect.h"
#import "IFIndexCell.h"
#import "IFIndexFocusCell.h"
#import "IFIndexIconCell.h"
#import "IFIndexSlidesCell.h"
#import "CLTopicImgCell.h"
#import "BlockUI.h"
#import "CLPushAnimatedRight.h"
#import "CLSlidersViewController.h"
#import "CLDocViewController.h"
#import "CLVideoViewController.h"
#import "CLTopicViewController.h"
#import "CLCollectModel.h"
#import "IFIndexAudioCell.h"

@interface CLCollectViewController ()

@property (nonatomic,strong)UIButton *backButton;
@property (nonatomic,strong)UIButton *edit;

@end

@implementation CLCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setImage:[UIImage imageNamed:@"tb_back.png"] forState:UIControlStateNormal];
    self.backButton.contentEdgeInsets=UIEdgeInsetsMake(7, 7, 7, 7);

    self.backButton.frame=CGRectMake(15.f, 20.f, 44.f, 44.f);
    __weak typeof(self) me=self;
    [self.backButton handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [me.dispatchObj pop];
    }];
    [self.view addSubview:self.backButton];
    
    self.edit=[UIButton buttonWithType:UIButtonTypeCustom];
    self.edit.titleLabel.font=[UIFont systemFontOfSize:19];
    self.edit.frame=CGRectMake(GlobleWidth-65.f, 32.f, 50.f, self.edit.titleLabel.font.lineHeight);
    [self.edit setTitleColor:RGB(98, 101, 113) forState:UIControlStateNormal];
    [self.edit setTitle:@"编辑" forState:UIControlStateNormal];
    [self.edit setTitle:@"完成" forState:UIControlStateSelected];
    [self.edit setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    
    [self.edit handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
      
            [((UIButton *)sender) setSelected:!me.tableView.isEditing];
        me.tableView.editing=!me.tableView.isEditing;
      
    }];
    
    
    [self.view addSubview:self.edit];
    
    UILabel *title=[[UILabel alloc] init];
    title.font=[UIFont systemFontOfSize:20];
    title.frame=CGRectMake((GlobleWidth-50.f)/2.f, 30.f, 50.f, title.font.lineHeight);
    title.textColor=RGB(98, 101, 113);
    title.textAlignment=NSTextAlignmentCenter;
    title.text=@"收藏";
    
    [self.view addSubview:title];
    
    CALayer *line=[CALayer layer];
    line.backgroundColor=title.textColor.CGColor;
    line.frame=CGRectMake(0, 64.f, GlobleWidth, 0.5);
    [self.view.layer addSublayer:line];
    
    [self.tableView hiddenHead];
    [self.tableView hiddenBottom:YES];
    CGRect frame=self.tableView.frame;
    frame.origin.y=64.f;
    frame.size.height=GlobleHeight-64.f;
    self.tableView.frame=frame;
    //self.tool=[self showToolbarWithType:0];
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"time" ascending:NO];
    NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:@"CLCollect"];
    //req.predicate = [NSPredicate predicateWithFormat:@"topicId=%@", self.itemId];
    req.sortDescriptors = @[sort];
    self.fetch = [[NSFetchedResultsController alloc] initWithFetchRequest:req
                                                     managedObjectContext:[IFCoreDataManager sharedInstance].mainMoc
                                                       sectionNameKeyPath:nil
                                                                cacheName:nil];
    self.fetch.delegate=self;
    [self.fetch performFetch:nil];
    
    if([self isViewLoaded])
        [self reloadTable];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CLCollect *list=[self.fetch objectAtIndexPath:indexPath];
    IFIndexCell *cell=[self cellForStyle:list.style tableView:tableView];
    [cell loadCollect:list];
    [cell setPlayVideo:list.link==CLList_video];
    cell.indexPath=indexPath;
    cell.delegate=self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CLCollect *newsList=[self.fetch objectAtIndexPath:indexPath];
    return newsList.cHeight;
}


- (IFIndexCell *)cellForStyle:(int)style tableView:(UITableView *)tableView{
    if(style==CLList_style_focus){
        static NSString *cellID=@"IFIndexFocusCell";
        IFIndexIconCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexIconCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }else if (style==CLList_style_doc_img){
        static NSString *cellID=@"IFIndexIconCell";
        IFIndexIconCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexIconCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }else if(style==CLList_style_doc){
        static NSString *cellID=@"IFIndexTitleCell";
        IFIndexTitleCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexTitleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }else if(style==CLList_style_slide){
        static NSString *cellID=@"IFIndexSlidesCell";
        IFIndexSlidesCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexSlidesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }else if(style==CLList_style_topic_img){
        static NSString *cellID=@"CLTopicImgCell";
        CLTopicImgCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[CLTopicImgCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }else if(style==CLList_style_audio){
        static NSString *cellID=@"IFIndexAudioCell";
        IFIndexAudioCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if(!cell){
            cell=[[IFIndexAudioCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
        
    }
    return [[IFIndexCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"IFIndexCell"];
}

- (BOOL)showPopAction{
    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CLCollect *list=[self.fetch objectAtIndexPath:indexPath];
    [self clickOnList:list];
}

- (void)didSelect:(NSIndexPath *)indexPath{
    CLCollect *list=[self.fetch objectAtIndexPath:indexPath];
    [self clickOnList:list];
    IFIndexCell *cell=(IFIndexCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    if(list.link!=CLList_sub)
        [cell setIsRead:YES];
}

- (void)clickOnList:(CLCollect *)list{
    if(list.link==CLList_sliders){
        CLSlidersViewController *slider=[CLSlidersViewController new];
        slider.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:slider];
        
    }else if(list.link==CLList_doc){
        CLDocViewController *doc=[CLDocViewController new];
        doc.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:doc];
    }else if(list.link==CLList_topic){
        CLTopicViewController *topic=[CLTopicViewController new];
        topic.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:topic];
    }else if(list.link==CLList_video){
        CLVideoViewController *video=[CLVideoViewController new];
        video.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:video];
    }else if(list.link==CLList_audio){
        CLDocViewController *doc=[CLDocViewController new];
        doc.isAudio=YES;
        doc.itemId=list.itemId;
        [[CLPushAnimatedRight sharedInstance] pushController:doc];
    }
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    CLCollect *list=[self.fetch objectAtIndexPath:indexPath];
    [[CLCollectModel sharedInstance] deleteItemId:list.itemId block:nil];
}



@end
